<?php

namespace Hilaoyu\LaravelExtend\Services\Auth\Console;

use Hilaoyu\LaravelExtend\Supports\Uuid;
use Illuminate\Console\Command;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Schema;

class InitDbCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'laravelExtend:authInitDb {conn?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Laravel extend Auth init database tables';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $conn_name = $this->argument('conn');
        $Db = DB::connection($conn_name);
        $Schema = Schema::connection($conn_name);//$Db->getSchemaBuilder();

        if(!$Schema->hasTable('auth_users')){
            $Schema->create('auth_users', function (Blueprint $table) {
                $table->char('id',36)->primary();
                $table->string('account')->unique()->comment('账号');
                $table->string('password')->comment('密码');
                $table->string('name')->nullable()->index()->comment('姓名,昵称');
                $table->string('mobile',20)->nullable()->unique()->comment('手机号');
                $table->string('email')->nullable()->unique()->comment('邮箱');
                $table->string('picture')->nullable()->default('')->comment('头像');
                $table->string('sex',6)->default('保密')->index()->comment('性别:男,女,保密');
                $table->date('date_of_birth')->nullable()->index()->comment('生日');
                $table->string('address')->default('')->comment('地址');
                $table->string('lang_code',10)->default('')->index()->comment('语言');
                $table->datetime('lock_to')->nullable()->comment('锁定时间');
                $table->string('last_login_platform')->default('')->index()->comment('最后登录平台');
                $table->string('last_login_source')->default('')->comment('最后登录标识');
                $table->string('last_login_ip')->default('')->comment('最后登录IP');
                $table->datetime('last_login_time')->nullable()->comment('最后登录时间');
                $table->text('summary')->nullable()->comment('简介');
                $table->string('reset_password_code_id',36)->default('')->comment('重置密码验证ID');
                $table->rememberToken();
                $table->datetime('created_at')->nullable();
                $table->datetime('updated_at')->nullable();
                $table->datetime('deleted_at')->nullable();
            });

            $Db->statement("ALTER TABLE `". $Db->getTablePrefix()."auth_users` comment '用户表'");

            $Db->table('auth_users')->insert([
                'id' => Uuid::generate(),
                'account' => 'admin',
                'password' => Hash::make('admin'),
                'name'     => 'Administrator',
            ]);
        }

        if(!$Schema->hasTable('auth_token')){
            $Schema->create('auth_token', function (Blueprint $table) {
                $table->char('id',36)->primary();
                $table->char('user_id',36)->index()->comment('用户ID');
                $table->string('model')->index()->comment('用户模型');
                $table->string('platform',60)->default('')->index()->comment('登录平台');
                $table->string('source',60)->default('')->comment('登录平台标识');
                $table->string('client_ip',60)->default('')->comment('登录IP');
                $table->string('expire_time')->comment('过期时间');
                $table->datetime('created_at')->nullable();
                $table->datetime('updated_at')->nullable();
            });

            $Db->statement("ALTER TABLE `". $Db->getTablePrefix()."auth_token` comment '登录令牌表'");
        }

        if(!$Schema->hasTable('auth_users_login_log')){
            $Schema->create('auth_users_login_log', function (Blueprint $table) {
                $table->char('id',36)->primary();
                $table->char('token_id',36)->index()->nullable()->comment('tokenID');
                $table->char('user_id',36)->index()->comment('用户ID');
                $table->char('app_id',36)->index()->nullable()->comment('应用ID');
                $table->string('user_model',100)->index()->comment('用户模型');
                $table->string('login_platform',60)->default('')->index()->comment('登录平台');
                $table->string('login_source',60)->default('')->comment('登录平台标识');
                $table->string('login_ip',60)->default('')->comment('登录IP');
                $table->string('referer',255)->default('')->comment('来源地址');
                $table->datetime('created_at')->nullable();
                $table->datetime('updated_at')->nullable();
            });

            $Db->statement("ALTER TABLE `". $Db->getTablePrefix()."auth_users_login_log` comment '登录令牌表'");
        }

        foreach (config('auth.providers', '') as $provider_config){
            if(data_get($provider_config,'provide_service')){
                if(!$Schema->hasTable('auth_apps')){
                    $Schema->create('auth_apps', function (Blueprint $table) {
                        $table->char('id',36)->primary();
                        $table->string('name')->default('')->comment('名称');
                        $table->string('secret',64)->comment('密钥');
                        $table->json('domain')->comment('域名');
                        $table->string('introduction')->nullable()->comment('介绍');
                        $table->string('logo')->nullable()->comment('LOGO');
                        $table->tinyInteger('status')->default(0)->index()->comment('状态');
                        $table->datetime('created_at')->nullable()->index()->comment('创建时间');
                        $table->datetime('updated_at')->nullable()->index()->comment('最后修改资料时间');
                        $table->datetime('deleted_at')->nullable()->comment('删除时间');
                    });

                    $Db->statement("ALTER TABLE `". $Db->getTablePrefix()."auth_apps` comment '登录应用授权表'");
                    break;
                }
            }
        }




        return  0;
    }
}
